package com.estar.jinyou.controller;


import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;
import org.apache.poi.ss.formula.functions.T;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.web.system.service.SystemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableLoadTimeWeaving;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.estar.app.business.mobileLogin.utils.ObjCopyUtils;
import com.estar.jinyou.entity.TmblEvalassistinfoEntity;
import com.estar.jinyou.entity.TmblEvallossinfoEntity;
import com.estar.jinyou.entity.TmblEvalpartinfoEntity;
import com.estar.jinyou.entity.TmblEvalrepairinfoEntity;
import com.estar.jinyou.entity.TmblEvalresponseEntity;
import com.estar.jinyou.vo.JInYouRequestVO;
import com.estar.jinyou.vo.JYReturnVO;
import com.estar.jinyou.vo.voEntity.EvalAssistInfoVO;
import com.estar.jinyou.vo.voEntity.EvalLossInfoVO;
import com.estar.jinyou.vo.voEntity.EvalPartInfoVO;
import com.estar.jinyou.vo.voEntity.EvalRepairInfoVO;
import com.estar.platform.caseInfo.entity.TmblCarInfoEntity;
import com.estar.platform.caseInfo.entity.TmblSurveyInfoEntity;

@Component("jinYouController")
@Scope("prototype")
public class JinYouController {
	@Autowired
	private SystemService systemService;
	private static final Logger logger = Logger.getLogger(JinYouController.class);
	
	public JYReturnVO doWork(JInYouRequestVO jy){
		String reportNo=null;
		String indexName=null;
		JYReturnVO  jyR=new JYReturnVO();
		try{
			
			reportNo=jy.getReportno();
			indexName=jy.getIndexname();
			
			//记录收到的信息，作为查询依据而已
			TmblEvalresponseEntity res=new TmblEvalresponseEntity();
			ObjCopyUtils.beansCopyFromJDBC(jy, res);
			res.setState("0");
			res.setCreateTime(new Date());
			this.systemService.save(res);
			
			String statecode=jy.getStatecode();
			
			if(statecode.equals("079")){//核损完成:更改状态和保存数据
				if (!updateLoss(jy,reportNo,indexName)) {
					jyR.setMessage("保存核损消息信息失败");
					jyR.setCode("0");
					return jyR;
				}
				if(!updateAssistInfo(jy,reportNo,indexName)){
					jyR.setMessage("保存辅料信息信息失败");
					jyR.setCode("0");
					return jyR;
				}
				if(!updatePartInfo(jy,reportNo,indexName)){
					jyR.setMessage("保存定损换件信息失败");
					jyR.setCode("0");
					return jyR;
				}
				if(!updateRepairInfo(jy,reportNo,indexName)){
					jyR.setMessage("保存定损修理信息失败");
					jyR.setCode("0");
					return jyR;
				}
			}
			
			if(!updateCarAndSur(statecode,reportNo,indexName)){
				jyR.setMessage("更新车辆和案件信息失败");
				jyR.setCode("0");
				return jyR;
			}
			
		}catch(Exception e){
			logger.error("updateLoss-work-的系统错误",e);
			e.printStackTrace();
		}	
		jyR.setMessage("成功");
		jyR.setCode("1");
		return jyR;
		
	}
	/*定损修理信息*/
	private boolean updateRepairInfo(JInYouRequestVO jy, String reportNo, String indexName) {
		boolean flag=false;
		try{
			EvalLossInfoVO loss=new EvalLossInfoVO();
			loss=jy.getEvalLossInfo();
			List<EvalRepairInfoVO> newRepairList=loss.getEvalRepairList();
			
			//查有无有保存到相关信息
			CriteriaQuery cq = new CriteriaQuery(TmblEvalrepairinfoEntity.class);
			cq.eq("report", reportNo);
			cq.eq("carindex", indexName);
			cq.add();
			List<TmblEvalrepairinfoEntity> repairList=this.systemService.getListByCriteriaQuery(cq, false);
			
			if(repairList!=null && repairList.size()>0){
				for(TmblEvalrepairinfoEntity a:repairList){
					this.systemService.delete(a);
				}
				for(EvalRepairInfoVO r:newRepairList){
					TmblEvalrepairinfoEntity evalrepairinfoEntity=new TmblEvalrepairinfoEntity();
					ObjCopyUtils.beansCopyFromJDBC(r, evalrepairinfoEntity);
					evalrepairinfoEntity.setReport(reportNo);
					evalrepairinfoEntity.setCarindex(indexName);
					this.systemService.save(evalrepairinfoEntity);
				}
			}else{
				for(EvalRepairInfoVO r:newRepairList){
					TmblEvalrepairinfoEntity evalrepairinfoEntity=new TmblEvalrepairinfoEntity();
					ObjCopyUtils.beansCopyFromJDBC(r, evalrepairinfoEntity);
					evalrepairinfoEntity.setReport(reportNo);
					evalrepairinfoEntity.setCarindex(indexName);
					this.systemService.save(evalrepairinfoEntity);
				}
			}
			flag=true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("updateLoss的系统错误1",e);
			flag=false;
		}
		return flag;
	}
	/*end----------------定损修理信息*/
	
	
	/*定损换件信息*/
	private boolean updatePartInfo(JInYouRequestVO jy, String reportNo, String indexName) {
		boolean flag=false;
		try{
			EvalLossInfoVO loss=new EvalLossInfoVO();
			loss=jy.getEvalLossInfo();
			List<EvalPartInfoVO> newPartList=loss.getEvalPartInfoList();
			
			//查有无有保存到相关信息
			CriteriaQuery cq = new CriteriaQuery(TmblEvalpartinfoEntity.class);
			cq.eq("report", reportNo);
			cq.eq("carindex", indexName);
			cq.add();
			List<TmblEvalpartinfoEntity> partList=this.systemService.getListByCriteriaQuery(cq, false);
			
			if(partList!=null && partList.size()>0){
				for(TmblEvalpartinfoEntity a:partList){
					this.systemService.delete(a);
				}
				for(EvalPartInfoVO p:newPartList){
					TmblEvalpartinfoEntity evalpartinfoEntity=new TmblEvalpartinfoEntity();
					ObjCopyUtils.beansCopyFromJDBC(p, evalpartinfoEntity);
					evalpartinfoEntity.setReport(reportNo);
					evalpartinfoEntity.setCarindex(indexName);
					this.systemService.save(evalpartinfoEntity);
				}
			}else{
				for(EvalPartInfoVO p:newPartList){
					TmblEvalpartinfoEntity evalpartinfoEntity=new TmblEvalpartinfoEntity();
					ObjCopyUtils.beansCopyFromJDBC(p, evalpartinfoEntity);
					evalpartinfoEntity.setReport(reportNo);
					evalpartinfoEntity.setCarindex(indexName);
					this.systemService.save(evalpartinfoEntity);
				}
			}
			flag=true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("updateLoss的系统错误2",e);
			flag=false;
		}
		return flag;
	}
	/* end ---------------定损换件信息*/
	
	/*辅料信息*/
	private boolean updateAssistInfo(JInYouRequestVO jy, String reportNo, String indexName) {
		boolean flag=false;
		try{
			EvalLossInfoVO loss=new EvalLossInfoVO();
			loss=jy.getEvalLossInfo();
			List<EvalAssistInfoVO> newAssisList=loss.getEvalAssitList();
			
			//查有无有保存到相关信息
			CriteriaQuery cq = new CriteriaQuery(TmblEvalassistinfoEntity.class);
			cq.eq("report", reportNo);
			cq.eq("carindex", indexName);
			cq.add();
			List<TmblEvalassistinfoEntity> assisList=this.systemService.getListByCriteriaQuery(cq, false);
			
			if(assisList!=null && assisList.size()>0){
				for(TmblEvalassistinfoEntity a:assisList){
					this.systemService.delete(a);
				}
				for(EvalAssistInfoVO e:newAssisList){
					TmblEvalassistinfoEntity evalassistinfoEntity=new TmblEvalassistinfoEntity();
					ObjCopyUtils.beansCopyFromJDBC(e, evalassistinfoEntity);
					evalassistinfoEntity.setReport(reportNo);
					evalassistinfoEntity.setCarindex(indexName);
					this.systemService.save(evalassistinfoEntity);
				}
			}else{
				for(EvalAssistInfoVO a:newAssisList){
					TmblEvalassistinfoEntity evalassistinfoEntity=new TmblEvalassistinfoEntity();
					ObjCopyUtils.beansCopyFromJDBC(a, evalassistinfoEntity);
					evalassistinfoEntity.setReport(reportNo);
					evalassistinfoEntity.setCarindex(indexName);
					this.systemService.save(evalassistinfoEntity);
				}
			}
			flag=true;
		}catch(Exception e){
			e.printStackTrace();
			logger.error("updateLoss的系统错误4",e);
			flag=false;
		}
		return flag;
	}
	/*end----------辅料信息*/
	
	/*案件信息和车辆信息*/
	private boolean updateCarAndSur(String statecode,String reportNo,String indexName){
		boolean flag=false;
		try{
			CriteriaQuery cq = new CriteriaQuery(TmblCarInfoEntity.class);
			cq.eq("reportNo", reportNo);
			cq.eq("indexNme", indexName);
			cq.add();
			List<TmblCarInfoEntity> carList=this.systemService.getListByCriteriaQuery(cq, false);
			
			CriteriaQuery cq2 = new CriteriaQuery(TmblSurveyInfoEntity.class);
			cq2.eq("reportNo", reportNo);
			cq2.add();
			List<TmblSurveyInfoEntity> surList=this.systemService.getListByCriteriaQuery(cq2, false);
			
			if(surList.size()>0 && surList!=null && carList.size()>0 && carList!=null){
				
				TmblSurveyInfoEntity surveyInfoEntity=surList.get(0);
				TmblCarInfoEntity carInfoEntity=carList.get(0);
				
				if(statecode.equals("071")){//待核损
					carInfoEntity.setEval("71");
				}else if (statecode.equals("079")){
					carInfoEntity.setEval("79");
				}else if (statecode.equals("057")) {
					carInfoEntity.setEval("57");
				}
				surveyInfoEntity.setEvalSur("t");
				systemService.updateEntitie(surveyInfoEntity);
				systemService.updateEntitie(carInfoEntity);
				
				flag=true;
			}else {
				flag=false;
				
			}
			
			
		}catch(Exception e){
			e.printStackTrace();
			logger.error("更新车辆的系统错误",e);
			flag=false;
		}
		return flag;
		
	}
	/*end------------案件信息和车辆信息*/
	
	/*核损消息*/
	private boolean updateLoss(JInYouRequestVO jy,String reportNo,String indexName) {
		boolean flag=false;
		try{
			EvalLossInfoVO loss=new EvalLossInfoVO();
			loss=jy.getEvalLossInfo();
			String sql="select id from tmbl_evallossinfo l where l.REPORT='"+reportNo+"' and l.CARINDEX='"+indexName+"'";
			List<String> idList=this.systemService.findListbySql(sql);
			if(idList!=null && idList.size()>0){
				TmblEvallossinfoEntity eloss=new TmblEvallossinfoEntity();
				CriteriaQuery cq = new CriteriaQuery(TmblEvallossinfoEntity.class);
				cq.eq("report", reportNo);
				cq.eq("carindex", indexName);
				cq.add();
				List<TmblEvallossinfoEntity> list=this.systemService.getListByCriteriaQuery(cq, false);
				eloss=list.get(0);
				ObjCopyUtils.beansCopyFromJDBC(loss, eloss);
				eloss.setUpdateTime(new Date());
				this.systemService.updateEntitie(eloss);
				flag=true;
				
			}else{
				TmblEvallossinfoEntity eloss=new TmblEvallossinfoEntity();
				ObjCopyUtils.beansCopyFromJDBC(loss, eloss);
				eloss.setCarindex(indexName);
				eloss.setReport(reportNo);
				eloss.setCreatetime(new Date());
				this.systemService.save(eloss);
				flag=true;
			}
			
		}catch(Exception e){
			e.printStackTrace();
			logger.error("updateLoss的系统错误3",e);
			flag=false;
		}
		return flag;
	}
	/* end---------核损消息*/
}
